package com.easybenefit.UUService.activity;


import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.easybeanfit.UUService.commutill.UUDialogUtill;
import com.easybeanfit.UUService.commutill.UUJsonUtil;
import com.easybeanfit.UUService.commutill.UUNetUtill;
import com.easybeanfit.UUService.commutill.UUSharedUtil;
import com.easybeanfit.UUService.commutill.UUStrUtil;
import com.easybeanfit.UUService.commutill.UUToastUtil;
import com.easybeanfit.UUService.config.UUConfig;
import com.easybeanfit.UUService.config.UUDict;
import com.easybenefit.UUService.BaseActivity;
import com.easybenefit.UUService.R;
import com.easybenefit.UUService.model.HttpUserBean;
import com.easybenefit.UUService.model.UserInfo;
import com.easybenefit.UUService.myinterface.IActivityInterface;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

public class LoginActivity extends BaseActivity implements IActivityInterface, View.OnClickListener {
    private Button btn_login;
    private EditText et_username, et_password;
    private TextView tv_phone, tv_top_title;
    private CheckBox checkbox;
    private TextView tv_rule;
    ProgressDialog progressDialog = null;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initData();
        initListener();
    }


    @Override
    public void initData() {
        tv_top_title.setText(getResources().getText(R.string.btn_login));
        context = this;
        btn_login.setEnabled(false);

    }

    @Override
    public void initView() {
        btn_login = (Button) findViewById(R.id.btn_login);
        et_password = (EditText) findViewById(R.id.et_password);
        et_username = (EditText) findViewById(R.id.et_username);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_top_title = (TextView) findViewById(R.id.top_title);
        checkbox = (CheckBox) findViewById(R.id.checkbox);
        tv_rule = (TextView) findViewById(R.id.tv_rule);
        btn_login.setBackgroundResource(R.drawable.btn_shape);
    }

    @Override
    public void initListener() {
        tv_phone.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        tv_rule.setOnClickListener(this);
        btn_login.setEnabled(true);
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    btn_login.setEnabled(true);
                    btn_login.setBackgroundResource(R.drawable.btn_shape);
                } else {
                    btn_login.setEnabled(false);
                    btn_login.setBackgroundResource(R.drawable.btn_shape_grey);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                String userName = et_username.getText().toString();
                String password = et_password.getText().toString();
                if (UUStrUtil.isEmpty(userName)) {
                    UUToastUtil.showToast(LoginActivity.this, "用户名不能为空");
                    break;
                }
                if (UUStrUtil.isEmpty(password)) {
                    UUToastUtil.showToast(LoginActivity.this, "密码不能为空");
                    break;
                }
                login(userName, password);
                break;
            case R.id.tv_phone:
                Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri
                        .parse("tel:" + tv_phone.getText().toString()));
                startActivity(callIntent);
                break;
            case R.id.tv_rule:
                Intent userintent = new Intent(context, UserAgreementActivity.class);
                startActivity(userintent);
        }
    }

    /**
     * 登录
     *
     * @param userName
     * @param password
     */
    private void login(String userName, final String password) {
        if (!UUNetUtill.isConnected(LoginActivity.this)) {
            UUToastUtil.showToast(LoginActivity.this, UUDict.NOT_INTERNET);

        } else {
            AsyncHttpClient client = new AsyncHttpClient();
            RequestParams params = new RequestParams();
            String url = UUConfig.SERVER_BASE_URL + "merlogin";
            params.put("username", userName);
            params.put("password", password);
            client.post(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onStart() {
                    progressDialog = UUDialogUtill.getLoadingBar(LoginActivity.this, "正在登录....");
                    progressDialog.show();
                }

                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    try {


                    String content = new String(bytes);
                    HttpUserBean responseBean = new HttpUserBean();
                    responseBean = (HttpUserBean) UUJsonUtil.fromJson(content, HttpUserBean.class);
                    responseBean.getData().setPassword(password);
                    if (!UUConfig.SUCCESS_CODE.equals(responseBean.getCode())) {
                        UUToastUtil.showToast(LoginActivity.this, responseBean.getMessage());
                    } else {
                        UUSharedUtil.saveObject(LoginActivity.this, UUConfig.UU_USERINFO_NAME, responseBean.getData());
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        LoginActivity.this.finish();
                    }
                    }catch (Exception e){
                        e.printStackTrace();
                        UUToastUtil.showToast(LoginActivity.this, "登录错误重新登录");
                    }
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

                }

                @Override
                public void onFinish() {
                    progressDialog.dismiss();
                }
            });
        }
    }
}
